﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DataAccess
{
    public class SQLDataAccess
    {
        public SqlConnection conn { get; set; }
        public string ConnectString { get; set; }
        public SQLDataAccess()
        {
            //this.ConnectString = @"Data Source=firis.com.vn;Initial Catalog=vnbanknet_test;uid=sa;pwd=huyhieu1314521";
            this.ConnectString = @"Data Source=firis.com.vn;Initial Catalog=vnbanknet_demo;uid=sa;pwd=huyhieu1314521";
            // this.ConnectString = @"Data Source=firis.com.vn;Initial Catalog=test_vnbanknet;uid=sa;pwd=huyhieu1314521";
            //  this.ConnectString = @"Data Source=firis.com.vn;Initial Catalog=vnbanknet_demo_04_05;uid=sa;pwd=huyhieu1314521";
            //  this.ConnectString = @"Data Source=MRLUN-PC;Initial Catalog=vnbanknet;Integrated Security=True";
            this.conn = new SqlConnection(ConnectString);
        }
        private void OpenConnect()
        {
            if (this.conn.State != ConnectionState.Open)
            {
                this.conn.Open();
            }
        }
        private void CloseConnect()
        {
            if (this.conn.State == ConnectionState.Open)
            {
                this.conn.Close();
            }
        }
        public DataTable exeSelect(string query)
        {
            this.OpenConnect();
            DataTable tb = null;
            try
            {
                SqlCommand cmd = new SqlCommand(query, this.conn);
                tb = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(tb);
                this.CloseConnect();
            }
            catch
            {
                tb = null;
            }
            return tb;
        }
        public int exeUpdate(string query)
        {
            this.OpenConnect();
            int kq = 0;
            SqlCommand cmd = new SqlCommand(query, this.conn);
            kq = cmd.ExecuteNonQuery();
            this.CloseConnect();
            return kq;
        }
    }
}
